package com.wfl.sampling.service.impl;

import com.wfl.sampling.anno.Log;
import com.wfl.sampling.dao.ActivityDao;
import com.wfl.sampling.dao.UserDao;
import com.wfl.sampling.entity.Activity;
import com.wfl.sampling.entity.User;
import com.wfl.sampling.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private UserDao userDao;
    /**
     * 返回所有列表中 的最新信息 相同订单只显示最新的
     */
    @Override
    //返回list（所有的活动）
    public List<Map<String, Object>> listAllActivity() {
        List<Activity> activities = activityDao.queryAllActivity();
        List<Map<String, Object>> activityDetails = new ArrayList<>();

        // 即使activities为空，也继续执行，构建并返回一个可能为空的activityDetails列表
        for (Activity activity : activities) {
            Map<String, Object> activityDetail = new HashMap<>();
            User user = null;
            if (activity.getInitiatorId() != null) {
                try {
                    user = userDao.getUserById(activity.getInitiatorId());
                } catch (NumberFormatException e) {
                    // 解析失败，user保持为null
                }
            }

            // user为null时提供默认值
            // 使用默认值表示未知用户
            Integer userId = user != null ? user.getId() : 10086;
            String username = user != null ? user.getUsername() : "Unknown";
            String avatar = user != null ? user.getAvatar() : "DefaultAvatarPath";

            activityDetail.put("activity_id", activity.getId() != null ? activity.getId() : -1);
            activityDetail.put("title", activity.getTitle() != null ? activity.getTitle() : "No Title");
            activityDetail.put("descrip", activity.getDescrip() != null ? activity.getDescrip() : "No Description");
            activityDetail.put("createTime", activity.getTime() != null ? activity.getTime() : "No Time Info");
            activityDetail.put("userid", userId);
            activityDetail.put("username", username);
            activityDetail.put("avatar", avatar);

            activityDetails.add(activityDetail);
        }
        // 返回可能为空的列表，但这表示查询成功完成
        return activityDetails;
    }
    @Override
    //按照条件查询
    public List<Activity> findActivityByCondition(Activity activity) {
        return activityDao.queryActivityByCondition(activity);
    }
    @Override
    //按照活动id查询活动
    public List<Activity> findActivityByActivityId(Integer ActivityId) {
        if (ActivityId != null) {
            try {
                return activityDao.queryActivityByActivityId(ActivityId);
            } catch (Exception e) {
                throw new RuntimeException("哦哦 出错咯!");
            }
        } else {
            throw new RuntimeException("活动编号不能为 空");
        }
    }
    @Override
    //修改活动
    public boolean modifyActivityById(Activity activity) {
        if (activity.getId() != null) {
            int effectNum = activityDao.updateActivityByactivityId(activity);
            if (effectNum > 0) {
                return true;
            } else{
                throw new RuntimeException("修改失败");
            }
        }else{
            throw new RuntimeException("订单编好为空");
        }
    }
    @Override
    @Log
    //活动添加
    public boolean addActivity(Activity activity) {
        if (activity.getTitle() != null && !"".equals(activity.getTitle()) && activity.getId() != null) {
            System.out.println("addActivity: "+activity);
            try{
                boolean effectedNum = activityDao.insertActivity(activity);
                // 在创建活动时添加活动最高等级者，即level0，发起者，默认普通参加者level为-1
                activityDao.insertActivityParticipant(activity.getId(),activity.getInitiatorId(),0);
                if(effectedNum){
                    return true;
                }else{
                    throw new RuntimeException("添加活动失败");
                }
            }catch (Exception e){
                throw new RuntimeException("添加活动失败");
            }
        } else {
            throw new RuntimeException("活动名或活动id不能为空");
        }
    }

    @Override
    public int findMaxId() {
        List<Activity> maxIdList = activityDao.findMaxId();
        if (maxIdList == null || maxIdList.isEmpty()) {
            // 如果没有找到任何活动，返回0作为初始ID
            return 0;
        }
        return maxIdList.get(0).getId() + 1;
    }
//    @Override
//    public int findMaxId(Activity activity) {
//
//        List<Activity> countActivityList = activityDao.queryActivityByConditionWithoutLimit(activity);
//        System.out.println(countActivityList);
//        return countActivityList.size();
//    }

  @Override
  //返回所有活动数量
    public int countActivity(Activity activity) {

        List<Activity> countActivityList = activityDao.queryActivityByConditionWithoutLimit(activity);
        System.out.println(countActivityList);
        return countActivityList.size();
    }
    //设置活动id
    public int setActivityId(Activity activity ){
        return activity.getId();
    }

    @Override
    public List<Activity> findActivitiesByActivityIds(List<Integer> activityIds) {
        return activityDao.findActivitiesByActivityIds(activityIds);
    }


    public String getActivityIdByUUId() {
        return " ";
    }
}
